<?php 
namespace app\Admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\Admin\model\Type;

class GoodsType extends Base
{
	/*类型展示*/
	public function GoodsType_list(){
		$result = Db('goods_type')->paginate();
		$this->assign('list',$result);
		return $this->fetch();
	}

	/*类型添加*/
	public function GoodsType_add(){
		$type = new Type();
		if(request()->isPost()){
			$data['type_name'] = input('post.type_name');
			$type_group = input('post.attr_group');
			$keyword_list = trim($type_group);
			$keyword_arr = explode("\r\n", $keyword_list);
			$data['attr_group'] = implode(',', $keyword_arr);
			$re = $type->addData($data);
			$this->redirect('GoodsType/GoodsType_list');
		}else{
			return $this->fetch();
		}
		
	}

	/*获取类型的属性分组*/
	public function GoodsType_check(){
		if(request()->isAjax()){
			$type_id = input('get.id');
			$re = Db('goods_type')->where('type_id',$type_id)->value('attr_group');
			if($re==null){
				$arr=[
					'code' => 0,
					'data' => ''
				];
			}else{
				$data = explode(',', $re);
				$arr=[
					'code' => 1,
					'data' => $data
				];
			}
			echo json_encode($arr);
		}
	}

	/*类型删除*/
	public function GoodsType_del(){
		$type_id = input('get.id');
		$result = db('goods_type')->where('type_id',$type_id)->delete();
		$re= db('attribute')->where('type_id',$type_id)->delete();
		if($result){
			if($re){
				$arr = [
					'code'=>1,
					'msg'=>'删除成功'
				];
			}else{
				$arr = [
					'code'=>2,
					'msg'=>'删除失败'
				];
			}
		}else{
			$arr = [
				'code'=>0,
				'msg'=>'删除失败'
			];
		}
		echo json_encode($arr);
	}

	/*类型显示修改*/
	public function GoodsType_show(){
		$id = input('get.id');
		$is_show = input('get.is_show');
		$result = Db('goods_type')->where('type_id',$id)->update(['is_show' => $is_show]);	
		echo json_encode($result);
	}

	/*类型名称修改*/
	public function GoodsType_save(){
		$tname = input('get.tname');
		$id = input('get.id');
		$result = Db('goods_type')->where('type_id',$id)->update(['type_name' => $tname]);
		echo json_encode($result);
	}


}//结尾